package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import artid.com.arttogoa.adapter.ReplyAdapter;
import artid.com.arttogoa.app.ArtWorkApp;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.ReplyParser;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.util.MySharedPreferences;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout.OnRefreshListener;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 
 * 
 * 项目名称：ArtworkArtA 类名称：ReplyActivity 类描述：回复列表页 创建人：耿卫斌 创建时间：2016年5月16日
 * 下午2:35:18
 * 
 */
public class ReplyActivity extends BaseActivity implements OnClickListener {
	private PullableListView mListView;
	private ReplyAdapter adapter;
	private Context mContext;
	private boolean isRefreshed = false;// 是否刷新
	private int page = 1;
	private List<ReplyParser> list;
	private List<ReplyParser> more_list;
	private PullToRefreshLayout mPullLayout;
	private LinearLayout noDataLL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reply);
		setContent(this, getIntent());
		mContext = this;
		initView();
	}

	private void initView() {
		//注册当前广播
		IntentFilter filter = new IntentFilter("com.Push.receiver");  
		registerReceiver(broadcastReceiver, filter);  
		list = new ArrayList<ReplyParser>();
		more_list = new ArrayList<ReplyParser>();
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("回复列表页");
		setBack();
		//将恢复未读状态恢复
		MySharedPreferences.getMySharedPreferences(mContext).setValue("isReplyPush"+AppUtil.getUser().getUid(), "false");
		noDataLL = (LinearLayout) findViewById(R.id.Reply_noData);
		mListView = (PullableListView) findViewById(R.id.Reply_listivew);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent in = new Intent(ReplyActivity.this,ArtDynamicDetailActivity.class);
				in.putExtra("id", more_list.get(position).getCom_art_id());
				//				Log.e("item", more_list.toString());
				startActivity(in);
			}
		});

		mPullLayout = (PullToRefreshLayout) findViewById(R.id.Reply_refresh_view);
		mPullLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				page = 1;
				isRefreshed = true;
				getReplyList();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				page++;
				getReplyList();
			}
		});
		getReplyList();
	}

	@Override
	public void onClick(View v) {
	}

	/**
	 * 获取通知列表
	 */
	private void getReplyList() {
		VolleyBasic.getInstance(ReplyActivity.this).post(Cache.replyList,
				VolleyMap.getInstance().replyList(page, 10), ReplyParser.class,
				new VolleyCallback() {
			@SuppressWarnings("unchecked")
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				mPullLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
				list = (List<ReplyParser>) vd.getList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				if (adapter == null) {
					adapter = new ReplyAdapter(ReplyActivity.this,
							more_list);
					mListView.setAdapter(adapter);
				}
				adapter.notifyDataSetChanged();
				Log.d("log", more_list.toString());
			}

			public void onError(VolleyError volleyError) {
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
			}
		});
	}

	//对开始接单的广播监听
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  

		@Override  
		public void onReceive(Context context, Intent intent) {  
			// TODO Auto-generated method stub  
			adapter=null;
			page=1;
			isRefreshed=true;
			if(ArtWorkApp.isForeground(mContext, "artid.com.arttogoa.ReplyActivity")){
				//将恢复未读状态恢复
				MySharedPreferences.getMySharedPreferences(mContext).setValue("isReplyPush"+AppUtil.getUser().getUid(), "false");
			}
			getReplyList();
		}
	};

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//关闭广播
		unregisterReceiver(broadcastReceiver);
	}

}
